﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class PossibleScheduleViewModelCollection
    {
        private List<PossibleScheduleViewModel> _lstSchedule;
        public PossibleScheduleViewModelCollection(StudentViewModel cVM, ClassTimeViewModel ctVM, icanDataContext dataContext)
        {
            _lstSchedule = new List<PossibleScheduleViewModel>();
            for (short k = 0; k < 7; k++)
            {
                PossibleScheduleViewModel oSVM = new PossibleScheduleViewModel(cVM, ctVM, k, dataContext);

                foreach (PossibleScheduleViewModel sVM in cVM.PosibleSchedules)
                {
                    if (sVM.GetHashCode() == oSVM.GetHashCode())
                    {
                        oSVM.IsSelected = true;
                        break;
                    }
                }
                _lstSchedule.Add(oSVM);
            }
        }
        public List<PossibleScheduleViewModel> Items
        {
            get
            {
                return _lstSchedule;
            }
        }
    }
    class AllPossibleScheduleViewModel : WorkspaceViewModel
    {
        private List<PossibleScheduleViewModelCollection> _lstScheduleViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _cancelCommand;
        private StudentViewModel _sVM;
        public AllPossibleScheduleViewModel(StudentViewModel studentVM, icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _sVM = studentVM;
            List<ClassTime> lstClassTime = _dataContext.ClassTimes.ToList();


            _lstScheduleViewModel = new List<PossibleScheduleViewModelCollection>();
            for (int i = 0; i < lstClassTime.Count; i++)
            {
                ClassTimeViewModel ctVM = new ClassTimeViewModel(_dataContext, lstClassTime[i]);
                PossibleScheduleViewModelCollection lstSVM = new PossibleScheduleViewModelCollection(studentVM, ctVM, _dataContext);//new List<PossibleScheduleViewModel>();
                _lstScheduleViewModel.Add(lstSVM);
            }


        }

        public List<PossibleScheduleViewModelCollection> AllSchedules
        {
            get { return _lstScheduleViewModel; }
            set
            {
                _lstScheduleViewModel = value;
                base.OnPropertyChanged("AllSchedules");
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(SaveSchedule);
                }
                return _saveCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }
        private void Cancel()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        private void SaveSchedule()
        {
            List<PossibleScheduleViewModel> lstSVMs = new List<PossibleScheduleViewModel>();
            foreach (PossibleScheduleViewModelCollection sVMCollection in _lstScheduleViewModel)
            {
                List<PossibleScheduleViewModel> lstSVM = sVMCollection.Items.Where(s => s.IsSelected == true).ToList();
                if (lstSVM != null && lstSVM.Count > 0)
                    lstSVMs.AddRange(lstSVM);
            }
            List<PossibleScheduleViewModel> sVMs = _sVM.PosibleSchedules.Where(s => lstSVMs.FindIndex(s1 => s1.GetHashCode() == s.GetHashCode()) < 0).ToList();
            foreach (PossibleScheduleViewModel sVM in sVMs)
            {
                _sVM.PosibleSchedules.Remove(sVM);
                if (sVM.ID > 0) //Update
                    sVM.Delete();
            }
            sVMs = lstSVMs.Where(s => _sVM.PosibleSchedules.FindIndex(s1 => s1.GetHashCode() == s.GetHashCode()) < 0).ToList();
            foreach (PossibleScheduleViewModel sVM in sVMs)
            {
                if (_sVM.ID > 0) //update
                    sVM.Save();
                _sVM.PosibleSchedules.Add(sVM);
            }

            Mediator.NotifyColleagues<List<PossibleScheduleViewModel>>(Messages.ListScheduleUpdated, lstSVMs);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }


    }
}
